﻿using UnityEngine;
using System.Collections;
using System.IO;
using facepp.detection;
public class ce : MonoBehaviour {

	// Use this for initialization
	void Start () {

      
        //通过图片URL反悔信息

      //  Facepp_detect.instance.api_key = "fb328953796a0393f607593db1a545c4";
      //  Facepp_detect.instance.api_secret = "gbQtE6UE2TFOp2VH2mAUU9wKLGssuYtx";

        StartCoroutine(Test());  
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public UITexture imgRgb;
   private WebCamTexture cameraTexture;
     string cameraName = "";
    private bool isPlay = false;

    IEnumerator Test()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            WebCamDevice[] devices = WebCamTexture.devices;
            cameraName = devices[0].name;
            cameraTexture = new WebCamTexture(cameraName, 960,540, 15);
            imgRgb.mainTexture = cameraTexture;
            cameraTexture.Play();
            isPlay = true;
        }
    }
    UITexture aa;
    void OnGUI()
    {
        if (isPlay)
        {
         //   GUI.DrawTexture(new Rect(0, 0, 400, 300), cameraTexture, ScaleMode.ScaleToFit);
           
        }
    }

    void OnClick()
    {
       // t.Pause();
        //这个方法就是保存图片
        StartCoroutine(getTexture2d());
        Debug.Log("截图成功");
    }
    int i = 0;
    IEnumerator getTexture2d()
    {
        yield return new WaitForEndOfFrame();
        Texture2D t = new Texture2D(Screen.width,Screen.height);//要保存图片的大小
        //截取的区域
        t.ReadPixels(new Rect(0, 10, Screen.width, Screen.height), 0, 0, false);
        t.Apply();
        //把图片数据转换为byte数组
        byte[] byt = t.EncodeToJPG();
        facePp(byt);
        //获取系统时间
        //var aa = System.DateTime.Now;
        //然后保存为图片
        File.WriteAllBytes(Application.dataPath + "/shexiang/" +Time.time + ".jpg", byt);
    }

    void facePp(byte[] byt)
    {
       // FaceDetectData data = Facepp_detect.instance.getFaceDetectData(byt);
      //  Debug.Log(data.faces.Count);
        FaceLandMarkData landmark_data = Facepp_landmark.instance.getLandMarkDataFromDetect(byt);
        Debug.Log(landmark_data.landmark.keyPoints.Count);
        Debug.Log(landmark_data.landmark.keyPoints[0].name + ":" + landmark_data.landmark.keyPoints[0].relative.x);
     //   Debug.Log(landmark_data.landmark.getP(LandMarkType.LEFT_EYE_PUPIL).x);
    }

   
}
